/* ----------------------------------------------------------------
 @name:     collapser - open and close boxes
 @version:  0.4
 @release:  2010-08-01
 @type:     jQuery plugin
 @author:   Jan Panschab
---------------------------------------------------------------- */

$(function() {

  $.fn.collapser = function(options) {
    var opts = $.extend({}, $.fn.collapser.defaults, options),
        selector = this.selector;

    return this.each(function() {

      var $box = $(this),
          boxPosition = opts.triggerPosition == 'prev' ? 'next' : 'prev',
          boxHeight = $box.height(),
          $trigger,
          $openBox,
          $hashBox;
      
      $box.data('height', boxHeight);
      
      if (opts.trigger == '') {
        $trigger = $box[opts.triggerPosition]();
      } else {
        var triggerTo = opts.triggerPosition == 'prev' ? 'before': 'after';
        $box[triggerTo]($(opts.trigger));
        $trigger = $box[opts.triggerPosition]();
      }

      // inicializace
      $box.hide().css({'height': 0}); // všechno se zavře
      $trigger.css({ 'cursor': 'pointer', 'text-decoration' : 'underline' });
      if (opts.openOnLoad) { // otevře se box podle selectoru
        $openBox = $box.filter(opts.openOnLoad);
        $openBox.css('height', $openBox.data('height') + 'px').show()[opts.triggerPosition](opts.trigger).addClass(opts.openClass);
      }
      if (document.location.hash) { // otevře se hash z adresy
        $hashBox = $(document.location.hash);
        if ($hashBox.data('height') !== undefined) {
          $hashBox
            .css('height', $hashBox.data('height') + 'px')
            .show()
            [opts.triggerPosition](opts.trigger)
            .addClass(opts.openClass);
        }
      }

      // event
      $trigger.click(function(e) {
        e.preventDefault();
        var $thisTrigger = $(this),
            $groupTriggers = $(selector)[opts.triggerPosition](),
            $thisBox = $thisTrigger[boxPosition](),
            thisBoxHeight = $thisBox.data('height');
        if (opts.autoClose) { // automaticky se zavírají ostatní boxy
          $groupTriggers.removeClass(opts.openClass).each(function() {
            $(this)[boxPosition]().not($thisBox).animate({height: 0}, opts.duration, opts.easing, function() {
              $(this).hide();
            });
          });
        }
        if ($thisBox.is(':visible')) { // close
          $thisTrigger.removeClass(opts.openClass);
          $thisBox.animate({height: 0}, opts.duration, opts.easing, function() {
            $thisBox.hide();
          });
          opts.onClose.call(this);
        } else { // open
          $thisTrigger.addClass(opts.openClass);
          $thisBox.show().animate({height: thisBoxHeight + 'px'}, opts.duration, opts.easing);
          opts.onOpen.call(this);
        }
      });

    });
  };
  // collapser defaults
  $.fn.collapser.defaults = {
    trigger: '', // [jQuery element] if empty, use prev or next element
    triggerPosition: 'prev', //[prev, next]
    openClass: 'topen', // [string] třída triggeru při otevřeném boxu
    autoClose: false, // [boolean] otevírání jednoho/více boxů
    openOnLoad: false, // [false|jQuery selector] co se otevře při načtení stránky
    duration: 1500, // [string(slow, normal, fast)|number] rychlost otevírání boxů
    easing: 'linear', // [linear, swing, or easing plugin]
    onOpen: function() {}, // [function] triggered after box is opened
    onClose: function() {} // [function] triggered after box is closed
  };

});
